package com.htu.courseService.auth.config;

import cn.dev33.satoken.stp.StpInterface;
import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.fastjson.JSON;
import com.htu.courseService.common.enums.RedisCachePrefix;
import com.htu.courseService.common.util.RedisCache;
import com.htu.courseService.user.vo.UserInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * Created By JNNarrator on 2024/9/7 17:55
 * email: jiangn09@chinatelecom.cn
 */
@Component
@RequiredArgsConstructor
public class StpInterfaceImpl implements StpInterface, RedisCachePrefix {
	private final RedisCache redisCache;
	@Override
	public List<String> getPermissionList(Object loginId, String loginType) {
		UserInfo userInfo = JSON.parseObject((String) StpUtil.getExtra("userInfo"),
				UserInfo.class);
		HashSet<String> permission = new HashSet<>();
		userInfo.getRolesAndPermissions().forEach(
				(k, v) -> permission.addAll(v)
		);
		return new ArrayList<>(permission);
	}
	
	@Override
	public List<String> getRoleList(Object loginId, String loginType) {
		UserInfo userInfo = JSON.parseObject((String) StpUtil.getExtra("userInfo"),
				UserInfo.class);
		return new ArrayList<>(userInfo.getRolesAndPermissions().keySet());
	}
}
